﻿package beautifulWorld2D.engine{

	
	import beautifulWorld2D.engine.Decor;
	import beautifulWorld2D.engine.interfaces.INiveau;
	import beautifulWorld2D.engine.Perso;
	import beautifulWorld2D.engine.controlePerso;
	import beautifulWorld2D.engine.interfaces.IPerso;
	import beautifulWorld2D.engine.bo.MemoryNiveau;
	import flash.display.MovieClip;
	import flash.display.Stage;
	import flash.events.Event;
	import flash.geom.Point;
	import flash.utils.getTimer;
	
	import beautifulWorld2D.engine.events.EngineEvent;

	public class Niveau implements INiveau{
		
		private var _mcClip:MovieClip;
		private var _containerDecor:MovieClip;
		private var _persoClip:MovieClip;
		private var _decor:Decor;
		private var _perso:Perso;
		private var _controlePerso:controlePerso;
		private var _memoryNiveau:MemoryNiveau;
		private var _stage:Stage;
		
		
		
		public function get memoryNiveau():MemoryNiveau
		{
			return _memoryNiveau;
		}
		public function get perso():IPerso
		{
			return _perso;
		}
		public function get clip():MovieClip
		{
			return _mcClip;
		}
		public function get containerDecor():MovieClip
		{
			return _containerDecor;
		}
		
		public function set positionPersoLabel(label:String)
		{
			//position _perso
			_perso.x = _mcClip.containerRotate.conteneur.getChildByName("position_" + label).x;
			_perso.y = _mcClip.containerRotate.conteneur.getChildByName("position_" + label).y;
			
			
		}
		
		public function get decor():Decor { return _decor; }
		
		public function set decor(value:Decor):void 
		{
			_decor = value;
		}
		
		public function get controlPerso():controlePerso { return _controlePerso; }
		
		//------------------------------------------------------------------------------------
		function Niveau($stage:Stage, clip:MovieClip, perso:MovieClip, memoryNiveau:MemoryNiveau = null)
		{
			_mcClip = clip;
			_stage = $stage;
			
			if (_memoryNiveau != null) 	_memoryNiveau = memoryNiveau;
			else 						_memoryNiveau = new MemoryNiveau();
			_memoryNiveau.init(this);
			
			_containerDecor = _mcClip.containerRotate.conteneur;
			_persoClip = perso;
			
			init();
		}
		//------------------------------------------------------------------------------------
		public function init(){
			

			trace("--> init Niveau");
			
			
			
			// creation _perso
			_perso = new Perso(_persoClip.perso, _persoClip.boxPerso, _persoClip.lensPerso, true);

			
			// position _perso
			_memoryNiveau.positionPerso = new Point(_mcClip.conteneur.position_start.x, _mcClip.conteneur.position_start.y);
			//_memoryNiveau.positionPerso = _mcClip.containerRotate.conteneur.localToGlobal(_memoryNiveau.positionPerso);
			_perso.x = _memoryNiveau.positionPerso.x;
			_perso.y = _memoryNiveau.positionPerso.y;


			// Creation _decor
			_decor = new Decor(_stage, _stage, _mcClip.conteneur, _mcClip.decor_fixe, _mcClip.decorMove);
			_decor.addPerso(_perso);
			
			
			//_mcClip.containerRotate.conteneur.addChild(_persoClip.lensPerso);
			//_mcClip.containerRotate.conteneur.addChild(_persoClip.perso);
			//_mcClip.containerRotate.conteneur.addChild(_persoClip.boxPerso);
			

			
			// init commande _perso
			_controlePerso = new controlePerso(_perso,_decor);
			

			Engine.addEventListener(EngineEvent.TIMER_GLOBAL, engineTime);
			_stage.addEventListener(Event.RESIZE, onResize);
			

			
			
			
			Engine.start();
			onResize();
			

			
		}
		
		private function onResize(e:Event = null):void 
		{
			_decor.onResize();
		}
		//------------------------------------------------------------------------------------
		public function engineTime(e:Event) {
			

			_controlePerso.listenerAction();
			_decor.gravityDecor();
		
			
		}

		//------------------------------------------------------------------------------------
		public function destroy(){
			trace("--> destroy class");
			
			_controlePerso.destroy();
			_decor.destroy();
			
			Engine.removeEventListener(EngineEvent.TIMER_GLOBAL, engineTime);
			_stage.removeEventListener(Event.RESIZE, onResize);
			
			_stage 			= null;
			_decor 			= null;
			_controlePerso 	= null;
			_perso 			= null;
			_persoClip 		= null;
			_mcClip 		= null;
			
			Engine.stop();
		}
	}
}